package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.bedManageDao;
import com.xueyu.domain.bed.bedBean;
import com.xueyu.domain.bed.pageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/open-bed")
public class bedManageController {

    @Autowired
    bedManageDao bedDao;

    //查询数据
    @RequestMapping(value = "select-bed")
    @ResponseBody
    public Map selectBed(){
        Map map = new HashMap();
        System.out.println("select-bed");
        List<bedBean> bedBeanList = bedDao.bedSelectAll();
        int count = bedDao.count();
        System.out.println("总记录数："+count);
        System.out.println(bedBeanList);
        map.put("bedList",bedBeanList);
        map.put("count",count);
        return map;
    }
    //新增
    @RequestMapping(value = "Add-bed")
    @ResponseBody
    public Map AddBed(String bedNO, String state, String remark){
        System.out.println("Add-bed");
        Map map = new HashMap();
        bedBean bedBean = new bedBean();

        int whereBedNo = bedDao.whereBedNo(Integer.valueOf(bedNO));
        if (whereBedNo >= 1){
            map.put("code",201);
            return map;
        }else {
            bedBean.setBedNO(bedNO);
            bedBean.setState(state);
            bedBean.setRemark(remark);
            int result = bedDao.bedAdd(bedBean);
            
            if (result != 0){
                System.out.println("新增true");
            }else{
                System.out.println("新增false");
            }
            map.put("code",200);
            return map;
        }
    }
    //获取指定的id，在进行操作
    @RequestMapping(value = "getID")
    @ResponseBody
    public Map getID(String cmd, int id) throws Exception {
        Map map = new HashMap();
        System.out.println(cmd);
        System.out.println("要删除或者修改的id是："+id);
        if (id == 1){
            System.out.println("\u001B[31mThis is a red text Error This data you do not have permission to operate\"");
            System.out.println("\u001B[0m");
            throw new Exception("NullPointerException");
        }
        if ("delete".equals(cmd)){
            int result = bedDao.deleteBed(id);
            if (result != 0){
                System.out.println("删除true");
            }else {
                System.out.println("删除false");
            }
        }else if ("toUpdate".equals(cmd)){
            List<bedBean> bedBeanList = bedDao.getID(id);
            map.put("bedList",bedBeanList);
        }
        return map;
    }

    //修改
    @RequestMapping(value = "Upd-bed")
    @ResponseBody
    public Map UpdBed(bedBean bedBean){
        System.out.println("Upd-bed");
        Map map = new HashMap();

        List<bedBean> selectAllBedNo = bedDao.selectAllBedNo();
        int bedId = bedBean.getBedID();
        List<bedBean> selectThisBedNo = bedDao.selectBedNoBy(bedId);
        boolean isMatched = false;
        int i = 0;
        for (bedBean bedNoList : selectAllBedNo){
            if (bedBean.getBedNO().equals(bedNoList.getBedNO())){
                isMatched = true;
                break;
            }
            i++;
            System.out.println("for"+i+"in not fund data false");
        }
        if (isMatched == false){
            System.out.println("this data bedManage bedNo true");
            int result = bedDao.bedUpdate(bedBean);
            if (result >= 1){
                System.out.println("修改true");
            }else {
                System.out.println("修改false");
            }
        }else {
            int UpdBedId = bedBean.getBedID();
            int whereBedIdBach = bedDao.whereBedIdBach(UpdBedId);
            boolean flag = false;
            for (bedBean bedNoList : selectAllBedNo){
                if (selectThisBedNo.get(0).getBedNO().equals(bedBean.getBedNO()) && UpdBedId == whereBedIdBach){
                    flag = true;
                    break;
                }
                i++;
                System.out.println("for"+i+"in not fund data false");
            }
            if (flag == false){
                map.put("code",201);
                System.out.println("this data bedManage bedNo false");
            }else {
                int result = bedDao.bedUpdate(bedBean);
                if (result >= 1){
                    System.out.println("修改true");
                }else {
                    System.out.println("修改false");
                }
            }
        }
        System.out.println(isMatched);
        return map;
    }

    //批量删除
    @RequestMapping(value = "deleteBatch-bed")
    @ResponseBody
    public void deleteBatchBed(String id, String cmd){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("bedId",id.split(","));
        System.out.println(cmd);
        System.out.println(queryWrapper);
        bedDao.delete(queryWrapper);
    }

    // 条件查询
    @RequestMapping(value = "selectCondition-bed")
    @ResponseBody
    public Map selectConditionBed(String like, HttpServletRequest req){
        Map map = new HashMap();
        System.out.println("selectCondition-bed");

        int curPage = Integer.valueOf(req.getParameter("curPage"));
        int pageSize = Integer.valueOf(req.getParameter("pageSize"));
        int count = bedDao.count();
        if(curPage <= 0) {
            curPage = 1;
        }
        int totalPages = (count + pageSize - 1) / pageSize;
        if(curPage > totalPages) {
            curPage = totalPages;
        }

        int offset = (curPage - 1) * pageSize;
//        if ("".equals(like) || like == null){
//            List<bedBean> bedBeanList = bedDao.selectPage(page);
//            map.put("bedList",bedBeanList);
//            map.put("count",count);
//        }

        List<bedBean> likes = bedDao.bedSelectLIKE(like,offset,pageSize);
        System.out.println(likes);
        System.out.println("like记录总数:"+likes.size());
        map.put("like",likes);
        map.put("count",likes.size());
        return map;
    }

    //分页查询
    @RequestMapping("/selectPage-bed")
    @ResponseBody
    public Map<String, Object> selectPageBed(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        int curPage = Integer.parseInt(req.getParameter("curPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String likes = req.getParameter("like");

        Page<bedBean> page = new Page<>(curPage,pageSize);
        QueryWrapper<bedBean> queryWrapper = new QueryWrapper<bedBean>();
        if (likes == null || likes == ""){
            page = bedDao.selectPage(page,null);
        }else {
            queryWrapper.like("bedNo",likes);
            queryWrapper.or().like("state",likes);
            queryWrapper.or().like("remark",likes);
            page = bedDao.selectPage(page,queryWrapper);
        }
        List<bedBean> bedBeanList = page.getRecords();

        map.put("bedList",bedBeanList);
        map.put("count",page.getTotal());

        return map;
    }


}

